﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;
namespace Miao
{
    public class PawnRenderNodeWorker_DraftMultiple : PawnRenderNodeWorker
    {

        public override bool CanDrawNow(PawnRenderNode node, PawnDrawParms parms)
        {
            if (!node.Props.rotDrawMode.HasFlag(parms.rotDrawMode))
            {
                return false;
            }

            if (node.Props.visibleFacing != null && !node.Props.visibleFacing.Contains(parms.facing))
            {
                return false;
            }

            if (node.Props.skipFlag != RenderSkipFlagDefOf.None && parms.skipFlags.HasFlag(node.Props.skipFlag))
            {
                return false;
            }

            if (node.hediff?.Part?.visibleHediffRots != null && !node.hediff.Part.visibleHediffRots.Contains(parms.facing))
            {
                return false;
            }

            Rot4 rot = Rot4.Invalid;
            if (node.Props.side != 0)
            {
                rot = ((node.Props.side == PawnRenderNodeProperties.Side.Left) ? Rot4.East : Rot4.West);
            }

            if (node.hediff?.Part != null && node.hediff.Part.def.IsMirroredPart)
            {
                rot = (node.hediff.Part.flipGraphic ? Rot4.East : Rot4.West);
            }

            if (rot != Rot4.Invalid && node.Props.flipGraphic && rot.IsHorizontal)
            {
                rot = rot.Opposite;
            }

            if (parms.facing == rot)
            {
                return false;
            }

            if (node.Props.linkedBodyPartsGroup != null && !parms.pawn.health.hediffSet.GetNotMissingParts().Any((BodyPartRecord x) => x.groups.NotNullAndContains(node.Props.linkedBodyPartsGroup)))
            {
                return false;
            }

            return true;
        }

        public override void AppendDrawRequests(PawnRenderNode node, PawnDrawParms parms, List<PawnGraphicDrawRequest> requests)
        {
            //Material material = GetMaterial(node, parms);
            //if (!(material == null))
            //{
            //    Mesh mesh = node.GetMesh(parms);
            //    if (!(mesh == null))
            //    {
            //        requests.Add(new PawnGraphicDrawRequest(node, mesh, material));
            //    }
            //}
            requests.Add(new PawnGraphicDrawRequest(node));
        }

        public override void PostDraw(PawnRenderNode node, PawnDrawParms parms, Mesh mesh, Matrix4x4 matrix)
        {
            Pawn pawn = parms.pawn;
            PawnKindLifeStage curKindLifeStage = pawn.ageTracker.CurKindLifeStage;
            if (!pawn.Drafted)
            {
                curKindLifeStage.bodyGraphicData.Graphic.Draw(pawn.DrawPos, pawn.Rotation, pawn);
            }
            else
            {
                curKindLifeStage.femaleGraphicData.Graphic.Draw(pawn.DrawPos, pawn.Rotation, pawn);
            }
        }
        //    public override bool CanDrawNow(PawnRenderNode node, PawnDrawParms parms)
        //    {
        //        if (!base.CanDrawNow(node, parms))
        //        {
        //            return false;
        //        }
        //        return true;
        //    }

        //    public override Vector3 OffsetFor(PawnRenderNode node, PawnDrawParms parms, out Vector3 pivot)
        //    {
        //        Vector3 result = base.OffsetFor(node, parms, out pivot);
        //        result.y = AltitudeFor(node, parms);
        //        return result;
        //    }

        //    public override void AppendDrawRequests(PawnRenderNode node, PawnDrawParms parms, List<PawnGraphicDrawRequest> requests)
        //    {
        //        requests.Add(new PawnGraphicDrawRequest(node));
        //    }

        //    public override void PostDraw(PawnRenderNode node, PawnDrawParms parms, Mesh mesh, Matrix4x4 matrix)
        //    {
        //        Vector3 pivot;
        //        Vector3 vector = parms.matrix.Position() + OffsetFor(node, parms, out pivot);
        //        if (parms.pawn.carryTracker?.CarriedThing != null)
        //        {
        //            PawnRenderUtility.DrawCarriedThing(parms.pawn, vector, parms.pawn.carryTracker.CarriedThing);
        //        }
        //        else
        //        {
        //            PawnRenderUtility.DrawEquipmentAndApparelExtras(parms.pawn, vector, parms.facing, parms.flags);
        //        }
        //    }
        }


    }